<html xmlns="http://www.w3.org/1999/xhtml">
<head>
    <title>Web报表(B/S报表)演示 - 以AJAX方式载入并保存设计的报表</title>
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8"/>
    <style type="text/css">
        html,body {
            margin:0;
            height:100%;
        }
    </style>

    <script src="../../../../scripts/boot.js" type="text/javascript"></script>

</head>
<body style="margin:0" onload="window_onload()">

<script src="../../../../scripts/CreateControl.js" type="text/javascript"></script>
<script src="../../../../scripts/GRUtility.js" type="text/javascript"></script>
<script type="text/javascript">
    var rowstr =localStorage.getItem('row');
    var thiseditrow =JSON.parse(rowstr);
    var ReportURL="";
    var viewArr=new Array();
    function window_onload()
    {

        if (typeof(thiseditrow.id)!="undefined" && thiseditrow.id !=0)
        {
            thiseditrow._state="modified";
            $.ajax({
                type:"post",
                url:ajaxUrl + "/printTemplate/getgrfstr?id="+thiseditrow.id+"&random="+Math.random(),
                success: function (text) {
                    if(text==""){
                        loadDesignView();
                    }else{
                        ReportDesigner.Report.LoadFromStr(text);
                        ReportDesigner.Reload();
                    }


                },
                error: function (jqXHR, textStatus, errorThrown) {
                    mini.alert(jqXHR.responseText);
                }
            });
        }else{
            thiseditrow._state="added";
        }
    }
    //加载设计视图
    function loadDesignView(){
        $.ajax({
            type:"post",
            url:ajaxUrl + "/formltems/findByModelId.do?modelId="+thiseditrow.cVouchID,
            success: function (text) {   //数据来源
                viewArr=text.formItem;
                CreateReportDesign(viewArr);
            },
            error: function (jqXHR, textStatus, errorThrown) {
                mini.alert("读取打印模板错误！");
                mini.alert(jqXHR.responseText);
            }
        });
    }
    var Report;
    //创建报告设计
    function CreateReportDesign(arr){
        Report = ReportDesigner.Report;

        //通过程序代码定义报表模板
        //<<
        Report.Clear();

        Report.Font.Point = 9; 	//定义报表主对象的属性

        var headerJson=new Array();
        var BodyJson=new Array();
        for(var i=0;i<arr.length;i++){
            var jsonObj=arr[i];
            var type =jsonObj.type;
            switch (type) {
                case 'tab_table':
                    BodyJson.push(jsonObj);
                    break;
                case 'tab':
                    break;
                default:
                    headerJson.push(jsonObj);
                    break;
            }
            // if(type=="tab_table"){
            //     BodyJson=jsonObj;
            // }else{
            //     headerJson.push(jsonObj);
            // }
        }

        DefinePageHeader(arr); //定义页眉
        DefinePageFooter(arr); //定义页脚


        DefineReportHeader(headerJson); //定义报表头
        DefineDetailGrid(BodyJson); //定义明细网格


        //>>

        //如果要检查定义的模板，可以保存到文件，然后用报表设计器打开查看
        //Report.SaveToFile("d:\\Program.grf");

        //对于360，搜狗等浏览器，必须加上这句才正常。深层次原因还不太清楚
        //运行到这里，ReportDesigner.Report 与 Report 已经不是同一个对象了
        ReportDesigner.Report = Report;

        //将用程序代码定义出来的报表模板载入进报表设计器，然后就可以进行可视化设计
        ReportDesigner.Reload();
    }
    function OnSaveReport()
    {
        ReportDesigner.Post();
        var cContent=ReportDesigner.Report.SaveToStr();
        thiseditrow.cContent=cContent;
        var arr=new Array();
        arr.push(thiseditrow);
        var json = mini.encode({"entityName": "PrintTemplate","data":arr});
        //var json = mini.encode({"entityName": "PrintTemplate","data":rowData});
        $.ajax({
            type:"post",
            url: ajaxUrl + "/printTemplate/insert.do",
            data: json,
            success: function (text) {
                mini.alert("模板保存成功！");
            },
            error: function (jqXHR, textStatus, errorThrown) {
                mini.alert(jqXHR.responseText);
            }
        });
        //忽略掉设计器本身的保存行为
        ReportDesigner.DefaultAction = false;
        if (window.CloseOwnerWindow)
            return window.CloseOwnerWindow("ok");
        else window.close();
    }

    function OnRequestData(Report)
    {

        Report.LoadDataFromURL(ajaxUrl + "/printTemplate/getgrfstr?id="+thiseditrow.id+"&random="+Math.random());
    }

    function DefinePageHeader(arr) {
        Report.InsertPageHeader();
        Report.PageHeader.Height = 0.58;

        //插入一个部件框
        var StaticBox = Report.PageHeader.Controls.Add(1).AsStaticBox; //GRControlType.grctStaticBox
        StaticBox.Text = "欢迎访问：www.GridppReport.com，了解最新产品信息";
        StaticBox.ForeColor = ColorFromRGB(0, 0, 255); //Red = 0, Green = 0, Blue = 255
        StaticBox.Left = 0;
        StaticBox.Top = 0;
        StaticBox.Width = 8.20;
        StaticBox.Height = 0.58;

        //插入另一个部件框
        StaticBox = Report.PageHeader.Controls.Add(1).AsStaticBox; //GRControlType.grctStaticBox
        StaticBox.Text = "Grid++Report 示例";
        StaticBox.Font.Point = 11;
        StaticBox.Font.Bold = true;
        StaticBox.Dock = 3; //GRDockStyle.grdsRight 使其锚定到页眉的右端
        StaticBox.TextFormat.TextAlign = 36; //GRTextAlign.grtaMiddleRight 使文本对齐到右端
        StaticBox.Width = 6;
    }

    function DefinePageFooter(arr)
    {
        Report.InsertPageFooter();
        Report.PageFooter.Height = 0.58;

        //插入一个系统变量框,显示页号
        var PageNoBox = Report.PageFooter.Controls.Add(3).AsSystemVarBox; //GRControlType.grctSystemVarBox
        PageNoBox.SystemVar = 3; //GRSystemVarType.grsvPageNumber
        PageNoBox.TextFormat.TextAlign = 36; //GRTextAlign.grtaMiddleRight
        PageNoBox.Left = 12.78;
        PageNoBox.Top = 0;
        PageNoBox.Width = 1.40;
        PageNoBox.Height = 0.40;

        //插入一个静态文本框,显示页号与总页数中间的分隔斜线字符'/'
        var StaticBox = Report.PageFooter.Controls.Add(1).AsStaticBox; //GRControlType.grctStaticBox
        StaticBox.Text = "/";
        StaticBox.TextFormat.TextAlign = 36; //GRTextAlign.grtaMiddleCenter
        StaticBox.Left = 14.18;
        StaticBox.Top = 0;
        StaticBox.Width = 0.40;
        StaticBox.Height = 0.40;

        //插入另一个系统变量框,显示页数
        var PageCountBox = Report.PageFooter.Controls.Add(3).AsSystemVarBox; //GRControlType.grctSystemVarBox
        PageCountBox.SystemVar = 2; //GRSystemVarType.grsvPageCount
        PageCountBox.Left = 14.58;
        PageCountBox.Top = 0;
        PageCountBox.Width = 1.40;
        PageCountBox.Height = 0.40;
    }
//定义报头
    function DefineReportHeader(arr) {
        Report.InsertDetailGrid();
        Report.DetailGrid.ColumnTitle.Height = 0.58;
        Report.DetailGrid.ColumnContent.Height = 0.58;
        var RecordSet = Report.DetailGrid.Recordset;
        if(arr.length>0){
            var Reportheader = Report.ReportHeaders.Add();
            var reportheader,
                controls,
                SubReport;
            var maxHight=0;
            for(var i=0;i<arr.length;i++){
                var jsonObj=arr[i];
                var StaticBox = Reportheader.Controls.Add(1).AsStaticBox; //GRControlType.grctStaticBox
                StaticBox.Text =jsonObj.cLadText;
                //  StaticBox.Center = 1; //GRCenterStyle.grcsHorizontal 使部件框在节中水平方向上居中对齐
                // StaticBox.Font.Point = 15;
                if(maxHight<(Report.PixelsToUnit(jsonObj.iConY)+Report.PixelsToUnit(jsonObj.iLabHeight))){
                    maxHight=Report.PixelsToUnit(jsonObj.iConY)+Report.PixelsToUnit(jsonObj.iLabHeight);
                }
                StaticBox.Top = Report.PixelsToUnit(jsonObj.iConY);
                StaticBox.Left= Report.PixelsToUnit(jsonObj.iConX);
                StaticBox.Width = Report.PixelsToUnit(jsonObj.iLabWidth);
                StaticBox.Height = Report.PixelsToUnit(jsonObj.iLabHeight);

                RecordSet.AddField(jsonObj.cTableField, 1);

                var FieldBox = Reportheader.Controls.Add(4).AsFieldBox;
                FieldBox.DataField = jsonObj.cTableField;
                FieldBox.Left =  StaticBox.Left+Report.PixelsToUnit(jsonObj.iLabWidth);
                FieldBox.Top = Report.PixelsToUnit(jsonObj.iConY);
                FieldBox.Width = Report.PixelsToUnit(jsonObj.iWidth);
                FieldBox.Height = Report.PixelsToUnit(jsonObj.iLabHeight);


            }
            Reportheader.Height = maxHight+1;
            //设计一个子报表



        }
        //
        //
        // //插入一个静态文本框,显示报表标题文字
        // var StaticBox = Reportheader.Controls.Add(1).AsStaticBox; //GRControlType.grctStaticBox
        // StaticBox.Text = "按单统计销售明细报表";
        // StaticBox.Center = 1; //GRCenterStyle.grcsHorizontal 使部件框在节中水平方向上居中对齐
        // StaticBox.Font.Point = 15;
        // StaticBox.Font.Bold = true;
        // StaticBox.Top = 0.40;
        // StaticBox.Width = 5.64;
        // StaticBox.Height = 0.58;
    }

    function DefineReportFooter()
    {
    }

    function DefineDetailGrid(BodyJson)
    {
        Report.DetailGrid.ColumnContent.AlternatingBackColor = ColorFromRGB(196, 196, 255);
        var gridColumns=BodyJson;
        var RecordSet = Report.DetailGrid.Recordset;
        for (var i=0;i<gridColumns.length;i++) {
            var col=gridColumns[i];
            RecordSet.AddField(col.cTableField, 1); //GRFieldType.grftInteger
            Report.DetailGrid.AddColumn(col.cTableField, col.cTableField_ch, col.cTableField, Report.PixelsToUnit(100));
        }
    }

    //报表模板载入URL、报表数据URL与报表模板保存URL这三个参数均不设置，而是用AJAX与WEB服务器传输数据，在浏览器中再调用报表插件的相关接口传输数据
    CreateDesignerEx("100%", "100%", ReportURL, "", "", "<param name='OnSaveReport' value='OnSaveReport'><param name='OnRequestData' value='OnRequestData'>");
</script>
</body>
</html>
